<?php
	/* Description: This file is used to add sub sub category. */
	#########IncludeFiles####################################################################################
	include_once '../inc/config.php';
	include_once("../inc/bootstrape.php");
	require_once _SITE_PATH_.'classes/Vendor/Vendor.php';
	require_once _SITE_PATH_.'classes/Category/Category.php';
	require_once _SITE_PATH_.'classes/Product/Product.php';
	require_once _SITE_PATH_.'classes/CommonUtility/CommonUtility.php';

	if( $_POST['submit']=='Submit'){    
		$parms['product_name'] 				= $_POST['product_name'];
		$parms['product_category']			= $_POST['product_category'];
		$parms['product_subcategory'] 		= $_POST['product_subcategory'];
		$parms['product_subsubcategory']	= $_POST['product_subsubcategory'];
		$parms['description'] 				= $_POST['description'];
		if(!empty($_POST['attributes'])){
			$parms['attributes'] 		= implode(',',$_POST['attributes']);
		}
		$parms['tags'] 						= $_POST['tags'];
		$parms['added']						= date("Y-m-d h:i:s");
		$parms['modified'] 					= date("Y-m-d h:i:s");
		//echo '<pre>';print_r($_POST);exit;
		// upload and create thumb of the image //
		if( !empty( $_FILES['product_image'] ) ){
			$filename  = 'product_image';     //  this is name of the control you are using in html form         
			$uploadedfile = CommonUtility::uploadNcreateThumb($filename, $module='product', true, 100);
			if( $uploadedfile!= -1 )    {
				$parms['product_image'] = $uploadedfile;//$_POST['package_image'];
			}        
		}
				
		if( $prodid = CommonUtility::insertData($parms, 'product',1)) {        
            //  add product attributes
			//below lines are comment for removing attributes from master product
//             $parms_attributes = array();
//             $parms_attributes['added'] = date('Y-m-d');
//             if(!empty($_POST['attributes'])){
//                 $parms_attributes['product_id'] = $prodid;
//                 foreach($_POST['attributes'] as $key => $val){
//                     $parms_attributes['attribute_id'] = $key;
//                     $parms_attributes['unit_id'] = $_POST['unit_'.$key];
//                     $unitRow = CommonUtility::getDataById($parms_attributes['unit_id'], 'unit_master');
//                     $parms_attributes['unit'] = $unitRow['title'] ;                                        
//                     foreach($val as $inner_key => $inner_val){
//                         $parms_attributes['attribute_name'] = $inner_key;
//                         $parms_attributes['attribute_value'] = $inner_val;
//                     }
//                     CommonUtility::insertData($parms_attributes, 'product_attributes');
//                 }
//             }
            
			header("Location: product_mng.php?msg=1204");            
			exit;
		}
		$err = 1205;    
	}

	$editor = CommonUtility::includeEditor();

	$main_categories = array();
	$main_categories = Category::getMainCategories();

	$sub_categories = array();
	$sub_categories = Category::getSubCategories();

	$sub_sub_categories = array();
	$sub_sub_categories = Category::getSubSubCategories();
	
	$drop_down_attributes = array();
	$drop_down_attributes = CommonUtility::getProductAttributes();

	$errMsg = $err;
	$_tplName = 'product/product_add.tpl';
	$_title = 'Add Product';

	require_once _ADMIN_TPL_MAIN_;
?>